/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-07-03
 * Time：13:49
 */
public class MyLinkedQueue {
    public Node head;
    public Node tail;

    public int usedSize;

    class Node {
        public int val;
        public Node next;
        public Node(int val) {
            this.val = val;
        }
    }

    /**
     * 出队
     * @return
     */
    public int poll() {
        if (isEmpty()) {
            throw new QueueEmptyException("队列为空，插入失败！");
        }
        int val = head.val;
        head = head.next;
        //只剩一个节点，需要处理一下tail节点
        if (head == null) {
            tail = null;
        }

        usedSize--;
        return val;
    }

    /**
     * 入队
     * @return
     */
    public void offer(int val) {
        Node node = new Node(val);
        if (head == null) {
            head = tail = node;
        } else {
            tail.next = node;
            tail = node;
        }
        usedSize++;
    }

    /**
     * 取对头元素
     * @return
     */
    public int peek() {
        if (isEmpty()) {
            throw new QueueEmptyException("队列为空，取元素失败！");
        }
        return head.val ;
    }

    public int size() {
        return usedSize;
    }
    
    public boolean isEmpty() {
        return usedSize==0;
    }
}
